/*
 * Copyright 2006 Martin Konzett <gekomic AT gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.toools.gwt.demo.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import java.util.List;
import org.toools.gwt.demo.jpa.Account;
import org.toools.gwt.demo.jpa.Container;
import org.toools.gwt.demo.jpa.Additive;
import org.toools.gwt.demo.jpa.Application;
import org.toools.gwt.demo.jpa.Company;
import org.toools.gwt.demo.jpa.CompanyPart;
import org.toools.gwt.demo.jpa.Disposer;
import org.toools.gwt.demo.jpa.Waste;

public class DemoEntryPoint implements EntryPoint {

    private final VerticalPanel vp = new VerticalPanel();


    public void onModuleLoad() {
        RootPanel.get().add(vp);

        DemoService.Util.getInstance().test( new AsyncCallback<String>() {

            public void onFailure(final Throwable caught) {
                System.err.println("service.test onFailure");
                caught.printStackTrace();
            }

            public void onSuccess(final String result) {
                vp.add(new HTML(result));
            }
        });

        DemoService.Util.getInstance().getAccounts(new SimpleCallback<Account>());
        DemoService.Util.getInstance().getAdditives(new SimpleCallback<Additive>());
        DemoService.Util.getInstance().getApplications(new SimpleCallback<Application>());
        DemoService.Util.getInstance().getCompanies(new SimpleCallback<Company>());
        DemoService.Util.getInstance().getCompanyParts(new SimpleCallback<CompanyPart>());
        DemoService.Util.getInstance().getContainers(new SimpleCallback<Container>());
        DemoService.Util.getInstance().getDisposers(new SimpleCallback<Disposer>());
        DemoService.Util.getInstance().getWaste(new SimpleCallback<Waste>());
    }

    private class SimpleCallback<T> implements AsyncCallback<List<T>> {

        public void onSuccess(final List<T> result) {
            if (result != null) {
                System.out.println("service onSuccess, result.size = " + result.size());
                for (final T t : result) {
                    vp.add(new HTML(t.toString()));
                }
            } else {
                System.out.println("service onSuccess, result = null");
            }
        }

        public void onFailure(final Throwable caught) {
            System.out.println("service onFailure");
            caught.printStackTrace();
        }
    }
}